package es.xpt.ysf.commons.business.view;

import java.util.List;

import javax.faces.component.UIData;

import org.apache.myfaces.component.html.ext.HtmlDataTable;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.commons.business.model.Period;
import es.xpt.ysf.commons.business.model.Remark;
import es.xpt.ysf.logging.LogManager;
import es.xpt.ysf.view.Controller;

@SuppressWarnings("serial")
public class RemarkController extends Controller {

	private Remark remark = new Remark();
	private Period period = new Period();

	private SimpleOnTheFlyRemarkController onTheFlyRemarkController;

	private List<Remark> remarkList;
	private List<Remark> remarkLanguages;
	private HtmlDataTable remarkInfo;

	private UIData periodInfo;

	private boolean insertRemarkLanguage = false;
	private boolean editRemarkLanguage = false;
	
	private String initDate;
	private String endDate;
	
	private Boolean viewFirstRemarks = false;
	
	//todas las observaciones-traducciones de bbdd
	private List<Remark> allRemarksLanguages;

	public RemarkController() {
		super();
		setMessageBundle("es.xpt.ysf.commons.business.Messages");
	}

	public RemarkController(SimpleOnTheFlyRemarkController onTheFlyRemarkController) {
		this();
		this.onTheFlyRemarkController = onTheFlyRemarkController;
	}

	public String editLanguages() {
		editRemarkLanguage = false;
		insertRemarkLanguage = false;
		period = new Period();
		Remark r = (Remark) remarkInfo.getRowData();
		if (r.getPeriod() != null)
			r.setPeriod(null);
		else
			remarkInfo.toggleDetail();
		return "";
	}

	public String insertRemark() {
		try {
			if (remark.getEntityID() == null) {
				if (onTheFlyRemarkController != null
						&& !onTheFlyRemarkController.containsOnTheFlyRemark(remark
								.getId())) {
					onTheFlyRemarkController.insertOnTheFlyRemark(remark);
				}			
				remarkInfo.collapseAllDetails();
			} else {
				new BusinessDelegate(getUserBean()).insertRemark(remark);
			}
			super.setInfoMessage("commons.remarks.insert");
			remark.setContent(null);
			remarkList = null;
			editRemarkLanguage = false;
			insertRemarkLanguage = false;
			viewFirstRemarks = false;
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
		return "success";
	}

	public String insertRemarkLanguage() {
		try {
			if (onTheFlyRemarkController != null
					&& onTheFlyRemarkController.containsOnTheFlyRemark(remark
							.getId())) {
				onTheFlyRemarkController.insertOnTheFlyRemarkLanguage(remark);
			} else {
				new BusinessDelegate(getUserBean())
						.insertRemarkLanguage(remark);
			}
			super.setInfoMessage("commons.remarks.insertLanguage");
			insertRemarkLanguage = false;
			editRemarkLanguage = false;
			remark.setContent(null);
			remarkList = null;
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}

		return "success";
	}

	public String deleteRemark() {
		try {
			Remark myRemark = (Remark) remarkInfo.getRowData();
			if (onTheFlyRemarkController != null
					&& onTheFlyRemarkController.containsOnTheFlyRemark(myRemark
							.getId())) {
				onTheFlyRemarkController.removeOnTheFlyRemark(myRemark.getId());
			} else {
				new BusinessDelegate(getUserBean()).deleteRemark(myRemark
						.getId());
			}
			super.setInfoMessage("commons.remarks.delete");
			remarkList = null;
			editRemarkLanguage = false;
			insertRemarkLanguage = false;
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
		return "success";
	}

	public String deleteRemarkLanguage() {
		try {
			if (onTheFlyRemarkController != null
					&& onTheFlyRemarkController.containsOnTheFlyRemark(remark
							.getId())) {
				onTheFlyRemarkController.removeOnTheFlyRemarkLanguage(remark
						.getId(), remark.getLocale());
			} else {
				new BusinessDelegate(getUserBean()).deleteRemark(
						remark.getId(), remark.getLocale());
			}
			super.setInfoMessage("commons.remarks.deleteLanguage");
			remark.setContent(null);
			remarkList = null;
			editRemarkLanguage = false;
			insertRemarkLanguage = false;
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
		return "success";
	}

	public String updateRemarkLanguage() {
		try {
			BusinessDelegate bd = new BusinessDelegate(getUserBean());
			bd.updateRemarkLanguage(remark);
			super.setInfoMessage("commons.remarks.updateLanguage");
			remarkList = null;
			editRemarkLanguage = false;
			insertRemarkLanguage = false;
			return "success";
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
	}

	/**
	 * @return the remarkList
	 */
	public List<Remark> getRemarkList() {
		List<Remark> onTheFlyRemarkList = null;
		if (onTheFlyRemarkController != null) {
			onTheFlyRemarkList = onTheFlyRemarkController
					.getOnTheFlyRemarkList();
		}
		if (onTheFlyRemarkList != null && onTheFlyRemarkList.size() > 0) {
			remarkList = onTheFlyRemarkList;
		} else if (remarkList == null && remark.getEntity() != null) {
			try {
				// if ( editPeriod )
				// remarkList = new
				// BusinessDelegate(getUserBean()).getRemarkLanguages(remark.getId());
				// else
				// if (showLocale == null || showLocale=="")
				// remarkList = new
				// BusinessDelegate(getUserBean()).getEntityRemarksAllLanguages(remark.getEntity(),
				// remark.getEntityID());
				// else
				remarkList = new BusinessDelegate(getUserBean())
						.getEntityRemarks(remark.getEntity(), remark
								.getEntityID());
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return remarkList;
	}

	public List<Remark> getRemarkLanguages() {
		try {
			Remark remarkAux = (Remark) remarkInfo.getRowData();
			if (remarkAux != null) {
				List<Remark> onTheFlyRemarkLanguages = null;
				if (onTheFlyRemarkController != null) {
					onTheFlyRemarkLanguages = onTheFlyRemarkController
							.getOnTheFlyRemarkLanguagesList(remarkAux.getId());
				}
				if (onTheFlyRemarkLanguages != null
						&& onTheFlyRemarkLanguages.size() > 0)
					remarkLanguages = onTheFlyRemarkLanguages;
				else
					try {
						remarkLanguages = new BusinessDelegate(getUserBean())
								.getRemarkLanguages(remarkAux.getId());
					} catch (YSFException e) {
						LogManager.getLog(getClass()).error(e.getMessage(), e);
					}
			}
		} catch (Throwable t) {
			LogManager.getLog(getClass()).error(t.getMessage(), t);
		}
		return remarkLanguages;
	}
	
	public List<Remark> getAllRemarksLanguages(){
		if (allRemarksLanguages == null && remark.getEntity() != null) {
			try {
				allRemarksLanguages = new BusinessDelegate(getUserBean())
						.getEntityRemarksAllLanguages(remark.getEntity(), remark
								.getEntityID());
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return allRemarksLanguages;
	}

	// PERIODOS

	public String editPeriods() {
		remark = (Remark) remarkInfo.getRowData();
		editRemarkLanguage = false;
		insertRemarkLanguage = false;
		period = new Period();
		if (remark != null && remark.getId() != null) {
			if (remarkInfo.isCurrentDetailExpanded()
					&& remark.getPeriod() != null) {
				remark.setPeriod(null);
				remarkInfo.toggleDetail();
			} else {
				try {
					if (onTheFlyRemarkController != null
							&& onTheFlyRemarkController
									.containsOnTheFlyRemark(remark.getId())) {
						remark.setPeriod(onTheFlyRemarkController
								.getOnTheFlyRemarkPeriodList(remark.getId()));
					} else {
						BusinessDelegate bd = new BusinessDelegate(
								getUserBean());
						remark.setPeriod(bd.getEntityPeriods("ysf_cb_remarks",
								remark.getId()));
					}
					if (!remarkInfo.isCurrentDetailExpanded())
						remarkInfo.toggleDetail();
					return "success";
				} catch (YSFException e) {
					LogManager.getLog(getClass()).error(e.getMessage(), e);
				}
			}
		}
		return "failure";
	}

	public String insertPeriod() {
		try {
			period.setEntity("ysf_cb_remarks");
			if (onTheFlyRemarkController != null
					&& onTheFlyRemarkController.containsOnTheFlyRemark(remark
							.getId())) {
				onTheFlyRemarkController.insertOnTheFlyRemarkPeriod(remark,
						period);
				remark.setPeriod(onTheFlyRemarkController
						.getOnTheFlyRemarkPeriodList(remark.getId()));
			} else {
				BusinessDelegate bd = new BusinessDelegate(getUserBean());
				bd.insertPeriod(period);
				remark.setPeriod(bd.getEntityPeriods("ysf_cb_remarks", remark
						.getId()));
			}
			super.setInfoMessage("commons.remarks.period.insert");
			period = new Period();
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
		return "success";
	}

	public String updatePeriod() {
		try {
			if (onTheFlyRemarkController != null
					&& onTheFlyRemarkController.containsOnTheFlyRemark(remark
							.getId())) {
				onTheFlyRemarkController.updateOnTheFlyRemarkPeriod(remark,
						period);
				remark.setPeriod(onTheFlyRemarkController
						.getOnTheFlyRemarkPeriodList(remark.getId()));
			} else {
				BusinessDelegate bd = new BusinessDelegate(getUserBean());
				bd.updatePeriod(period);
				remark.setPeriod(bd.getEntityPeriods("ysf_cb_remarks", remark
						.getId()));
			}
			super.setInfoMessage("commons.remarks.period.update");
			period = new Period();
			return "success";
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
	}

	public String deletePeriod() {
		try {
			Remark myRemark = (Remark) remarkInfo.getRowData();
			Period myPeriod = (Period) periodInfo.getRowData();
			if (onTheFlyRemarkController != null
					&& onTheFlyRemarkController.containsOnTheFlyRemark(myRemark
							.getId())) {
				onTheFlyRemarkController.deleteOnTheFlyRemarkPeriod(myRemark,
						myPeriod);
				myRemark.setPeriod(onTheFlyRemarkController
						.getOnTheFlyRemarkPeriodList(myRemark.getId()));
			} else {
				BusinessDelegate bd = new BusinessDelegate(getUserBean());
				bd.deletePeriod(myPeriod.getId());
				myRemark.setPeriod(bd.getEntityPeriods("ysf_cb_remarks", remark
						.getId()));
			}
			super.setInfoMessage("commons.remarks.period.delete");
			return "success";
		} catch (YSFException e) {
			super.setErrorMessage(e);
			LogManager.getLog(getClass()).error(e.getMessage(), e);
			return "failure";
		}
	}

	// GETTERS AND SETTERS

	/**
	 * @param remarkList
	 *            the remarkList to set
	 */
	public void setRemarkList(List<Remark> remarkList) {
		this.remarkList = remarkList;
	}

	/**
	 * @return the remark
	 */
	public Remark getRemark() {
		return remark;
	}

	/**
	 * @param remark
	 *            the remark to set
	 */
	public void setRemark(Remark remark) {
		this.remark = remark;
	}

	/**
	 * @return the period
	 */
	public Period getPeriod() {
		return period;
	}

	/**
	 * @param period
	 *            the period to set
	 */
	public void setPeriod(Period period) {
		this.period = period;
	}

	/**
	 * @return the editRemarkLanguage
	 */
	public boolean isEditRemarkLanguage() {
		return editRemarkLanguage;
	}

	/**
	 * @param editRemarkLanguage
	 *            the editRemarkLanguage to set
	 */
	public void setEditRemarkLanguage(boolean editRemarkLanguage) {
		this.editRemarkLanguage = editRemarkLanguage;
	}

	/**
	 * @return the remarkInfo
	 */
	public HtmlDataTable getRemarkInfo() {
		return remarkInfo;
	}

	/**
	 * @param remarkInfo
	 *            the remarkInfo to set
	 */
	public void setRemarkInfo(HtmlDataTable remarkInfo) {
		this.remarkInfo = remarkInfo;
	}

	/**
	 * @return the periodInfo
	 */
	public UIData getPeriodInfo() {
		return periodInfo;
	}

	/**
	 * @param periodInfo
	 *            the periodInfo to set
	 */
	public void setPeriodInfo(UIData periodInfo) {
		this.periodInfo = periodInfo;
	}

	/**
	 * @param remarkLanguages
	 *            the remarkLanguages to set
	 */
	public void setRemarkLanguages(List<Remark> remarkLanguages) {
		this.remarkLanguages = remarkLanguages;
	}

	/**
	 * @return the insertRemarkLanguage
	 */
	public boolean isInsertRemarkLanguage() {
		return insertRemarkLanguage;
	}

	/**
	 * @param insertRemarkLanguage
	 *            the insertRemarkLanguage to set
	 */
	public void setInsertRemarkLanguage(boolean insertRemarkLanguage) {
		this.insertRemarkLanguage = insertRemarkLanguage;
	}

	public SimpleOnTheFlyRemarkController getOnTheFlyRemarkController() {
		return onTheFlyRemarkController;
	}

	public void setOnTheFlyRemarkController(
			SimpleOnTheFlyRemarkController onTheFlyRemarkController) {
		this.onTheFlyRemarkController = onTheFlyRemarkController;
	}

	public String getInitDate() {
		String day = period.getInitDay() == null ? "" : (period.getInitDay()
				.toString().length() == 1 ? "0"
				+ period.getInitDay().toString() : period.getInitDay()
				.toString())
				+ "/";
		String month = period.getInitMonth() == null ? "" : (period
				.getInitMonth().toString().length() == 1 ? "0"
				+ period.getInitMonth().toString() : period.getInitMonth()
				.toString())
				+ "/";
		String year = period.getInitYear() == null ? "" : period.getInitYear()
				.toString();
		initDate = day + month + year;
		return initDate;
	}

	public String getEndDate() {
		String day = period.getEndDay() == null ? "" : (period.getEndDay()
				.toString().length() == 1 ? "0" + period.getEndDay().toString()
				: period.getEndDay().toString())
				+ "/";
		String month = period.getEndMonth() == null ? "" : (period
				.getEndMonth().toString().length() == 1 ? "0"
				+ period.getEndMonth().toString() : period.getEndMonth()
				.toString())
				+ "/";
		String year = period.getEndYear() == null ? "" : period.getEndYear()
				.toString();

		endDate = day + month + year;
		return endDate;

	}
	
	public String cancelEditing() {
		this.viewFirstRemarks = false;
		return "sucess";
	}	

	public void setInitDate(String initDate) {
		this.initDate = initDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the viewFirstRemarks
	 */
	public Boolean getViewFirstRemarks() {
		return viewFirstRemarks;
	}

	/**
	 * @param viewFirstRemarks the viewFirstRemarks to set
	 */
	public void setViewFirstRemarks(Boolean viewFirstRemarks) {
		this.viewFirstRemarks = viewFirstRemarks;
	}

	public void setAllRemarksLanguages(List<Remark> allRemarksLanguages) {
		this.allRemarksLanguages = allRemarksLanguages;
	}

	/*
	 * public String getShowLocale() { return showLocale; }
	 * 
	 * 
	 * public void setShowLocale(String showLocale) { this.showLocale =
	 * showLocale; }
	 */

}
